package com.xy.manage.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.xy.common.page.Pagination;
import com.xy.core.dao.impl.BaseDaoImpl;
import com.xy.manage.dao.TsurveyanswerDao;
import com.xy.manage.model.Statistics;
import com.xy.manage.model.Tsurveyanswer;
import com.xy.manage.model.Tsurveyquestion;

public class TsurveyanswerDaoImpl extends BaseDaoImpl<Tsurveyanswer> implements TsurveyanswerDao {
	public TsurveyanswerDaoImpl() {
		super(Tsurveyanswer.class);
	}
   
   
	@Override
	public List<Tsurveyanswer> getTsurveyanswer(Map<String, Object> map) {
		Object[] values = map.values().toArray();
		List<Tsurveyanswer> localList = findByHql(getHql(map)+" order by answerid desc", values);
		return localList;
	}

	@Override
	public Pagination getTsurveyanswerByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		Object[] values = map.values().toArray();
		return findByPage(getHql(map)+" order by answerid desc", values, pageNo, pageSize);
	}

	private String getHql(Map<String, Object> map) {
		StringBuffer str = new StringBuffer("from Tsurveyanswer tt");
		if(map.size()>0)str.append(" where ");
		Set<String> set = map.keySet();
		StringBuffer where = new StringBuffer();
		for (String name : set) {
			if (where.length() > 0)
				where.append(" and  ");
			where.append("tt.").append(name).append("=?");
		}
		return str.append(where.toString()).toString();
	}


	@Override
	public Tsurveyquestion getCount(Tsurveyquestion tsurveyquestion) {
		String hql = "select answer,count(*) as count from Tsurveyanswer t where t.tsurveyquestion.questionid = ? group by t.answer";
		Object[] values = new Object[]{tsurveyquestion.getQuestionid()};
		List<Object[]> localList = findByHql(hql, values);
		if(null==localList||localList.size()<=0)
			return tsurveyquestion;
		else{
			List<Statistics> temp = new ArrayList<Statistics>();
			for(Object[] os : localList){
				Statistics s = new Statistics(os[1].toString(), os[0].toString());
				temp.add(s);
			}
			tsurveyquestion.setStatisticses(temp);
			return tsurveyquestion;
		}
	}   
}